package com.hmall.user.controller;


import com.hmall.api.user.userDto.User;
import com.hmall.user.domain.dto.LoginDto;


import com.hmall.user.domain.vo.UserLoginVO;
import com.hmall.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;


    @PostMapping("/login")
    public UserLoginVO login(@RequestBody @Validated LoginDto loginDTO) {
        return userService.login(loginDTO);
    }


    //新增用户
    @PostMapping
    public void saveUser(@RequestBody User user) {
        userService.saveUser(user);
    }


    //根据Id查询用户
    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getUserById(id);
    }


    //根据Id删除用户
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable("id") Long id) {
        userService.removeUserById(id);
    }


    //修改用户
    @PutMapping
    public void updateUser(@RequestBody User user) {
        userService.updateUser(user);
    }


    //根据Ids批量查询用户
    @GetMapping("/ids/{ids}")
    public List<User> getUserByIds(@PathVariable("ids") Long[] ids) {
        return userService.getUserByIds(ids);
    }

}

